Makes an empty set that can hold up to capacity different values. example is any value of the kind it will hold - set_create(16, 0) is a set
set_create(capacity, example) -> set<T>| Name | Type |
|---|---|
capacity | int |
example | element |
set<T>
Makes an empty set that can hold up to capacity different values. set_create(16, 0) is a set of whole numbers with room for 16.
capacity is the most the set will ever hold, and it has to be a power of two: 2, 4, 8, 16, 32 and so on, never 10 or 30. That rule comes from the container the Game Boy Advance builds a set out of, and it is the same whichever way you write the size. If you pick another number, the compiler tells you the next size up.
It also has to be a number you type, like 16, not a variable, because a GBA cannot hand out memory while a game runs.
example tells GBML what kind of thing goes in the set. Give any value of that kind: 0 for whole numbers, 0.0 for decimals, "" for text, true for yes-or-no. The set starts empty, and the example is never added to it. It is only read to learn the kind. A struct cannot go in a set, so it cannot be the example either.
It sets the size of the variable it is given to, so it goes on the right-hand side of the line that creates the variable: var seen = set_create(16, 0);.
Leave the size out and you get 8.
set_create(16, 0) means the same as var seen: set<int, 16> = []; and the /// @capacity 16 comment. See list_create for the three ways side by side.
var seen = set_create(16, 0); // an empty set of whole numbers
set_add(seen, 5);
set_add(seen, 5); // 5 is already there, so nothing changes
var count = set_length(seen); // 1Compiles to an empty fixed-size hash set of `capacity` items, the same storage `/// @type {set<T>}` + `/// @capacity capacity` gives. A hash set only compiles with a power-of-two size (the container checks this when the game is built), so any other number is refused and the next power of two up is suggested. Leave the size out and a set holds 8. The size has to be a plain number because it becomes a C++ template parameter: the Game Boy Advance has no heap to hand out memory while the game runs, so every container is sized when the ROM is built (anything else is refused). It must be the whole right-hand side of a declaration, because that is the one place a size can be attached to a variable. The example is only read for its kind - it is never evaluated, never stored and never put in the container.